package project;

import java.awt.EventQueue;


import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;
import java.io.*;
import javax.swing.JOptionPane;
import java.net.*;
import java.lang.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;
import java.awt.*;
import javax.swing.*;


public class Student_Login {

	public JFrame frame;
	private JTextField User_field;
	private JPasswordField passwordField;

	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Student_Login window = new Student_Login();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	
	public Student_Login() {
		initialize();
	}

	
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(400, 200, 500, 400);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel lblLoginDetails = new JLabel("LOGIN DETAILS");
		lblLoginDetails.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblLoginDetails.setHorizontalAlignment(SwingConstants.CENTER);
		lblLoginDetails.setBounds(10, 28, 464, 38);
		frame.getContentPane().add(lblLoginDetails);
		
		JLabel lblNewLabel = new JLabel("User Name");
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setBounds(39, 120, 132, 14);
		frame.getContentPane().add(lblNewLabel);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblPassword.setHorizontalAlignment(SwingConstants.CENTER);
		lblPassword.setBounds(39, 161, 132, 31);
		frame.getContentPane().add(lblPassword);
		
		User_field = new JTextField();
		User_field.setBounds(237, 119, 173, 20);
		frame.getContentPane().add(User_field);
		User_field.setColumns(10);
		
		JButton btnLogin = new JButton("LOGIN");
		btnLogin.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				
				btnLoginActionPerformed(evt);
			}
		});
		btnLogin.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnLogin.setBounds(199, 267, 89, 23);
		frame.getContentPane().add(btnLogin);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(237, 168, 173, 20);
		frame.getContentPane().add(passwordField);
	}
	
	public int getUser_Pass(String user_name, String pass)
	{
		String st1;
		int flag = 0;
		st1= "select * from login"; 
		try
        {
            Class.forName("java.sql.Driver");
            
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3307/oats","root","anupam24");
            
            Statement stmt = con.createStatement();
            
            ResultSet rs = stmt.executeQuery(st1);
           
            String user = "";
            String password = "";
            
            while (rs.next())
            {
            	
            	 user = rs.getString(1);
            	 password = rs.getString(2);
            	 
            	 if(user.equals(user_name) && password.equals(pass))
            	 {
            		
         			flag =1;		// for checking whether the username and password is correct or not, if its 0 then not found
         			         			
            	 }
            }
            
        }
		catch(Exception e)
        {
            System.out.println("Error is : "+e.getMessage());
        }
		
		return flag;
	}

	
	
	private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {
				
		String user_name = "";
		String pass = "";
		
		user_name = User_field.getText();
		pass = new String(passwordField.getPassword());
		
		int flag = getUser_Pass(user_name,pass);
		
		if(flag == 1)
   	 	{
			Test_Details td = new Test_Details();
			
			td.frame.setVisible(true);
			
			td.display(user_name);   //for diplaying the username on next window
			
			this.frame.setVisible(false);
   	 	}
		
		if (flag == 0)
        {
        	
    			JOptionPane.showMessageDialog(null, " User ID / Password is Wrong\n","Login Fail", JOptionPane.ERROR_MESSAGE);
    		
        }
		
		/*new StopWatch().addWindowListener(new WindowAdapter()
        {
        public void windowClosing(WindowEvent e)
            {
            System.exit(0);
        }
    });*/
		
		/*String st1;
		st1= "select * from login"; 
		try
        {
            Class.forName("java.sql.Driver");
            
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3307/oats","root","anupam24");
            
            Statement stmt = con.createStatement();
            
            ResultSet rs = stmt.executeQuery(st1);
           
            String user = "";
            String password = "";
            int flag = 0;
            while (rs.next())
            {
            	
            	 user = rs.getString(1);
            	 password = rs.getString(2);
            	 
            	 if(user.equals(user_name) && password.equals(pass))
            	 {
            		Test_Details td = new Test_Details();
         			
         			td.frame.setVisible(true);
         			
         			flag =1;		// for checking whether the username and password is correct or not, if its 0 then not found
         			
         			td.display(user_name);   //for diplaying the username on next window
         			
         			this.frame.setVisible(false);
            	 }
            }
            
            if (flag == 0)
            {
            	
        			JOptionPane.showMessageDialog(null, " User ID / Password is Wrong\n","Login Fail", JOptionPane.ERROR_MESSAGE);
        		
            }
            
            stmt.close();
            con.close();
        }
        catch(Exception e)
        {
            System.out.println("Error is : "+e.getMessage());
        }*/
        
      //this code will get the IP address of the client machine. The file containing the user id and password of the user
	  //will be saved by the name of the ip address so as server could easily identify on which machine which user gave
	  //exam test.
        
        String Ip = "";
		try		
		{
		    InetAddress ownIP=InetAddress.getLocalHost();
		    
		    Ip = ownIP.getHostAddress();
		    System.out.println(Ip);
		}
		catch (Exception e)
		{
			System.out.println("Exception is ="+e.getMessage());
		}
		
		// Create file with ip address as the name
		
		String filename = Ip + ".txt";
		
		try
		{
			  FileWriter fstream = new FileWriter(filename);
			  BufferedWriter out = new BufferedWriter(fstream);
			  out.write(user_name);
			  out.write(pass);
			  out.close();
		}
		catch (Exception e)
		{
			  System.err.println("Error is: " + e.getMessage());
		}
	}
}
